﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using SmartSpace.UI.Silverlight.Dynamic.DynamicRef;
using System.Text;
using System.Windows.Markup;

namespace SmartSpace.UI.Silverlight.Dynamic.Utils
{
    /// <summary>
    /// 列表模版公用操作类
    /// </summary>
    public class ListTemplateUtil
    {
        public static void Build(DataGrid dataGrid, ViewMetadata viewMetadata)
        {
            //if (viewProcessStep)
            //{
            //    DataGridTextColumn column = new DataGridTextColumn();
            //    column.Header = "状态";
            //    column.Binding = new System.Windows.Data.Binding();
            //    column.Binding.Path = new PropertyPath("StatusMessage");
            //    dataGrid.Columns.Add(column);
            //}

            bool firstColumn = true;
            foreach (FieldMetadata field in viewMetadata.FieldMetadatas)
            {
                if (field.CanVisit)
                {
                    if (firstColumn)
                    {
                        firstColumn = false;
                        DataGridTemplateColumn column = new DataGridTemplateColumn();
                        column.Header = field.Caption;

                        //使用代码增加列
                        StringBuilder sbTemplateXaml = new StringBuilder();
                        sbTemplateXaml.Append("<DataTemplate ");
                        sbTemplateXaml.Append("xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ");
                        sbTemplateXaml.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
                        sbTemplateXaml.Append("xmlns:ss='clr-namespace:SmartSpace.UI.Silverlight;assembly=SmartSpace.UI.Silverlight' ");
                        sbTemplateXaml.Append("xmlns:src='clr-namespace:SmartSpace.UI.Silverlight.Dynamic.Commands;assembly=SmartSpace.UI.Silverlight.Dynamic' ");
                        //sbTemplateXaml.Append("xmlns:ds='clr-namespace:DemeterSoft.Silverlight;assembly=DemeterSoft.Silverlight' ");
                        sbTemplateXaml.Append(">");
                        sbTemplateXaml.Append("<Grid>");
                        sbTemplateXaml.Append("<Grid.Resources>");
                        sbTemplateXaml.Append("<src:DetailEntryCommand x:Key=\"DetailEntryCommand\" />");
                        sbTemplateXaml.Append("</Grid.Resources>");
                        sbTemplateXaml.Append("<HyperlinkButton Content=\"{Binding Path=["+field.Name+"]}\" Command=\"{StaticResource DetailEntryCommand}\" CommandParameter=\"{Binding RelativeSource={RelativeSource Self}}\" Tag=\"{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ss:AppView}}\" VerticalAlignment=\"Center\"/>");
                        sbTemplateXaml.Append("</Grid>");
                        sbTemplateXaml.Append("</DataTemplate>");
                        column.CellTemplate = XamlReader.Load(sbTemplateXaml.ToString()) as DataTemplate;
                        //Grid grid = column.CellTemplate.LoadContent() as Grid;
                        //HyperlinkButton button = grid.Children[0] as HyperlinkButton;
                        //button.Click += new RoutedEventHandler(HyperlinkButton_Click);
                        dataGrid.Columns.Add(column);
                    }
                    else
                    {
                        DataGridTextColumn column = new DataGridTextColumn();
                        column.Header = field.Caption;
                        column.Binding = new System.Windows.Data.Binding();
                        column.Binding.Path = new PropertyPath("[" + field.Name + "]");
                        dataGrid.Columns.Add(column);
                    }
                }
            }

            // 添加流程流转情况按钮

            DataGridTemplateColumn wfColumn = new DataGridTemplateColumn();
            wfColumn.Header = "流转情况";

            StringBuilder sbWfTemplateXaml = new StringBuilder();
            sbWfTemplateXaml.Append("<DataTemplate ");
            sbWfTemplateXaml.Append("xmlns='http://schemas.microsoft.com/winfx/2006/xaml/presentation' ");
            sbWfTemplateXaml.Append("xmlns:x='http://schemas.microsoft.com/winfx/2006/xaml' ");
            sbWfTemplateXaml.Append("xmlns:ss='clr-namespace:SmartSpace.UI.Silverlight;assembly=SmartSpace.UI.Silverlight' ");
            sbWfTemplateXaml.Append("xmlns:src='clr-namespace:SmartSpace.UI.Silverlight.Dynamic.Commands;assembly=SmartSpace.UI.Silverlight.Dynamic' ");
            //sbTemplateXaml.Append("xmlns:ds='clr-namespace:DemeterSoft.Silverlight;assembly=DemeterSoft.Silverlight' ");
            sbWfTemplateXaml.Append(">");
            sbWfTemplateXaml.Append("<Grid>");
            sbWfTemplateXaml.Append("<Grid.Resources>");
            sbWfTemplateXaml.Append("<src:WfHistoryCommand x:Key=\"WfHistoryCommand\" />");
            sbWfTemplateXaml.Append("</Grid.Resources>");
            sbWfTemplateXaml.Append("<HyperlinkButton Content=\"查看流程\" Command=\"{StaticResource WfHistoryCommand}\" CommandParameter=\"{Binding RelativeSource={RelativeSource Self}}\" Tag=\"{Binding RelativeSource={RelativeSource FindAncestor, AncestorType=ss:AppView}}\" VerticalAlignment=\"Center\"/>");
            sbWfTemplateXaml.Append("</Grid>");
            sbWfTemplateXaml.Append("</DataTemplate>");
            wfColumn.CellTemplate = XamlReader.Load(sbWfTemplateXaml.ToString()) as DataTemplate;
            
            //Grid grid = column.CellTemplate.LoadContent() as Grid;
            //HyperlinkButton button = grid.Children[0] as HyperlinkButton;
            //button.Click += new RoutedEventHandler(HyperlinkButton_Click);
            dataGrid.Columns.Add(wfColumn);
        }
    }
}
